#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 6e5 + 10;

int n;
int h[N], e[N], ne[N], idx;
int cnt[N];
int f[N][2];
int res;

void add(int a, int b){
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx ++;
}

void init(){
    idx = 0;
    for(int i = 0; i <= n; i ++){
        h[i] = -1;
        cnt[i] = 0;
        f[i][0] = f[i][1] = -1;
    }

    
    res = 0;
}


int calc(int u, int fa){
    cnt[u] = 1;

    for(int i = h[u]; ~i; i = ne[i]){
        int j = e[i];
        if(j == fa) continue;
        cnt[u] += calc(j, u);
    }

    return cnt[u];
}

void dfs(int u, int fa){

    if(f[u][1] != -1) return;

    int son[2], scnt = 0;
    for(int i = h[u]; ~i; i = ne[i]){
        int j = e[i];
        if(j == fa) continue;
        son[scnt++] = j;
    }

    if(scnt == 0) f[u][1] = 0;
    else if(scnt == 1){
        f[u][1] = cnt[son[0]] - 1;
    }else{ //scnt == 2
        dfs(son[0], u), dfs(son[1], u);
        f[u][1] = max(f[son[0]][1] + cnt[son[1]] - 1, f[son[1]][1] + cnt[son[0]] - 1);
    }

}

void solve(){
    cin >> n;
    init();

    int a, b;
    for(int i = 1; i < n; i ++){
        cin >> a >> b;
        add(a, b);
        add(b, a);
    }

    calc(1, -1);

    dfs(1, -1);
    cout << f[1][1] << '\n';
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    int j = 1;
    cin >> T;
    while(T--){
        solve();
    }


    return 0;
}